package com.example.phoneshop.controller;

import com.example.phoneshop.entity.User;
import com.example.phoneshop.service.UserService;
import com.example.phoneshop.tool.Md5;
import com.example.phoneshop.tool.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "login")
public class LoginController {
    private final UserService userService;

    @Autowired
    public LoginController(UserService userService){
        this.userService = userService;
    }

    @RequestMapping(value = {"index", "/", ""}, method = RequestMethod.GET)
    public ModelAndView index()
    {
        ModelAndView model = new ModelAndView("login/index");
        model.addObject("title", "登录");
        return model;
    }

    @RequestMapping(value = "loginHandle", method = RequestMethod.POST)
    public @ResponseBody ResultJson loginHandle(@RequestParam("username") String phone, @RequestParam("password") String password, HttpSession session){
        ResultJson resultJson = new ResultJson();
        String error = "用户名或密码不正确";
        String correct = "ok";
        User user = userService.findUserByPhone(phone);
        try{
            if(user == null){
                resultJson.setText(error);
            }else{
                if(user.getPassword().equals(Md5.get(password))){
                    user.setPassword(null);
                    session.setAttribute("user", user);
                    resultJson.setText(correct);
                }else{
                    resultJson.setText(error);
                }
            }
        }catch (Exception e){
            resultJson.setText(error);
        }
        return resultJson;
    }

    @RequestMapping(value = "register", method = RequestMethod.GET)
    public ModelAndView register(){
        ModelAndView model = new ModelAndView("login/register");
        model.addObject("title", "注册用户");
        return model;
    }

    @RequestMapping(value = "registerHandle", method = RequestMethod.POST)
    public @ResponseBody ResultJson registerHandle(@RequestParam("name[]") String[] name, @RequestParam("value[]") String[] value){
        ResultJson resultJson = new ResultJson();
        Map<String, String> map = new HashMap<>();
        int j = 0;
        for (String v:name){
            if(v.equals("password")){
                // md5加密
                value[j] = Md5.get(value[j]);
                map.put(v, value[j++]);
            }else{
                map.put(v, value[j++]);
            }
        }
        // 首先判断用户是否存在(根据手机号)
        User user = null;
        try{
            user = userService.findUserByPhone(map.get("phone"));
            if(user.getUsername() != null){
                resultJson.setText("注册失败，用户已经存在！");
                return resultJson;
            }
        }catch (Exception e){
//            e.printStackTrace();
        }
        try{
            user = userService.findUserByEmail(map.get("email"));
            if(user.getUsername() != null){
                resultJson.setText("注册失败，邮箱已被占用！");
                return resultJson;
            }
        }catch (Exception e){
//            e.printStackTrace();
        }
        // 将密码加密
        map.put("registertime", Calendar.getInstance().getTimeInMillis() + "");
        map.put("power", "3");
        int result = userService.insertUser(map);
        if(result == 1){
            resultJson.setText("ok");
        }else{
            resultJson.setText("注册失败，请稍后再试");
        }
        return resultJson;
    }

    @RequestMapping(value = "loginOut", method = RequestMethod.GET)
    public @ResponseBody ResultJson loginOut(HttpSession session){
        ResultJson resultJson = new ResultJson();
        session.invalidate();
        resultJson.setText("ok");
        return resultJson;
    }

    @RequestMapping(value = "getUserStatus", method = RequestMethod.GET)
    public @ResponseBody ResultJson getUserStatus(HttpSession session){
        ResultJson resultJson = new ResultJson();
        try{
            User user = (User)session.getAttribute("user");
            resultJson.setText(user.getId() + "");
        }catch (Exception e){
            resultJson.setText(null);
        }
        return resultJson;
    }
}















